package com.ai.aiagent.entity;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
import org.springframework.ai.chat.messages.Message;

import java.util.List;

@Entity
@Data
@Table(name = "mysql_chat_memory")
public class MysqlChatMemory {

    @Id
    private Long chatMemoryConversationId;

    @Column(name = "prompt_type", nullable = false)
    private String promptType;

    @Column(name = "message_info", nullable = false, columnDefinition = "JSON") // MySQL专用类型
    private String messageInfo;

    // 将List转为JSON存储
    public void setMessages(List<Message> messages) throws JsonProcessingException {
        this.messageInfo = JSONUtil.toJsonStr(messages);
    }

    // 从JSON读取List
    public List<Message> getMessages() throws JsonProcessingException {
        if (messageInfo == null) return List.of();
        return JSONUtil.toList(messageInfo, Message.class);
    }
}
